package com.jincp.filepicker.adapter;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.utils.ToastUtil;
import com.jincp.filepicker.R;
import com.jincp.filepicker.bean.ImageFile;
import com.jincp.filepicker.ui.image.ImagePickerManager;
import com.jincp.image.load.ImageLoader;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

/**
 * @ProjectName: filepicker.
 * @ClassName: ImagePickAdapter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/3/22 21:44.
 */
public class ImagePickAdapter extends BaseQuickAdapter<ImageFile, BaseViewHolder> {

    private int maxSelectedLimit = ImagePickerManager.getInstance().getMaxSelectedLimit();

    private int currentSelectedCount = 0;

    public ImagePickAdapter() {
        super(R.layout.item_image_pick);
    }

    @Override
    protected void convert(final BaseViewHolder holder, final ImageFile item) {
        if (item == null) {
            holder.setVisible(R.id.iv_camera, true);
            holder.setVisible(R.id.iv_thumbnail, false);
            holder.setVisible(R.id.iv_cbx, false);
        } else {
            ImageLoader.getInstance().displayImage(item.getPath(), (ImageView) holder.getView(R.id.iv_thumbnail));
            if (ImagePickerManager.getInstance().isContainSelected(item)) {
                ((ImageView) holder.getView(R.id.iv_thumbnail)).setColorFilter(Color.parseColor("#80000000"));
            } else {
                ((ImageView) holder.getView(R.id.iv_thumbnail)).clearColorFilter();
            }
            holder.setVisible(R.id.iv_camera, false);
            holder.setVisible(R.id.iv_thumbnail, true);
            holder.setVisible(R.id.iv_cbx, ImagePickerManager.getInstance().isMultiMode());
            holder.setSelected(R.id.iv_cbx, ImagePickerManager.getInstance().isContainSelected(item));
            holder.getView(R.id.iv_cbx).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    doSelectedImage(item, holder);
                }
            });
        }
    }

    private void doSelectedImage(ImageFile item, BaseViewHolder holder) {
        if (!ImagePickerManager.getInstance().isContainSelected(item) && isOverMaxSelectedLimit()) {
            ToastUtil.getInstance().show("已达到选择上限");
            return;
        }
        if (ImagePickerManager.getInstance().isContainSelected(item)) {
            ImagePickerManager.getInstance().removeSelectedFile(item);
            holder.setSelected(R.id.iv_cbx, false);
            ((ImageView) holder.getView(R.id.iv_thumbnail)).clearColorFilter();
            currentSelectedCount--;
        } else {
            ImagePickerManager.getInstance().addSelectedFile(item);
            holder.setSelected(R.id.iv_cbx, true);
            ((ImageView) holder.getView(R.id.iv_thumbnail)).setColorFilter(Color.parseColor("#80000000"));
            currentSelectedCount++;
        }
    }

    public boolean isOverMaxSelectedLimit() {
        return currentSelectedCount >= maxSelectedLimit;
    }
}
